package com.qjc.demo.proxy.controller;

import com.qjc.demo.proxy.service.DynamicProxyHandle;
import com.qjc.demo.proxy.service.SingService;
import com.qjc.demo.proxy.service.impl.StaticProxyServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Proxy;

/**
 * @ClassName: TestProxyController
 * @Description:
 * @Author: qiaojiacheng
 * @Date: 2021/8/24 4:19 下午
 */
@RestController
@RequestMapping("/proxy")
public class TestProxyController {


    @Autowired
    private StaticProxyServiceImpl staticProxyService;
    @Autowired
    private DynamicProxyHandle dynamicProxyHandle;

    /**
     * http://localhost:8081/proxy/static/test
     * 调用的时候，客户端是不知道有歌手存在的，只知道找到代理人就能唱歌
     */
    @RequestMapping("/static/test")
    public String testStaticProxy() {
        return staticProxyService.sing();
    }

    /**
     * http://localhost:8081/proxy/dynamic/test
     * 调用的时候，客户端是不知道有歌手存在的，只知道找到代理人就能唱歌
     */
    @RequestMapping("/dynamic/test")
    public String testDynamicProxy() {
        SingService singerService = (SingService) Proxy.newProxyInstance(ClassLoader.getSystemClassLoader(), new Class[]{SingService.class}, this.dynamicProxyHandle);
        return singerService.sing();
    }

}
